﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formBudgetWork : FormMDIChildTemplate
    {
        public formBudgetWork()
        {
            InitializeComponent();
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            reportProductPriceList reportProductPriceList = new reportProductPriceList();
            reportProductPriceList.bindingSourceCategory.DataSource = context.ProductCategories;

            reportProductPriceList.xrLabelFirm.Lines = db.GetFirmOwn().GetFullDescription();
            reportProductPriceList.ShowPreview();
        }

        WorkCategory currentCategory;

        private void formMaterialProduct_Load(object sender, EventArgs e)
        {
            root.TranslateGrid(gridWork);
            unitBindingSource.DataSource = context.Units.ToList();
            LoadCategory();
            if (treeViewCategory.Nodes.Count > 0)
            {
                currentCategory = (WorkCategory)treeViewCategory.Nodes[0].Tag;
                LoadProducts();
            }
        }

        private void LoadProducts()
        {
            textBoxCategoryName.Text = currentCategory.WorkCategoryName ;
            var works = context.Works.Where(x => x.WorkCategory == currentCategory);
            workBindingSource.DataSource = works;
        }

        #region LoadCategory
        private void LoadCategory()
        {
            treeViewCategory.Nodes.Clear();
            var rootCategories = context.WorkCategories .Where(x => x.ParentID == null);
            foreach (WorkCategory category in rootCategories) InsertNode(null, category);
        }

        private void InsertNode(TreeNode node, WorkCategory category)
        {
            TreeNode treeNode = new TreeNode();
            treeNode.Tag = category;
            treeNode.Text = category.WorkCategoryName;
            if (node == null) treeViewCategory.Nodes.Add(treeNode);
            else node.Nodes.Add(treeNode);
            foreach (WorkCategory subCategory in category.WorkCategories) InsertNode(treeNode, subCategory);
        }
        #endregion

        private void treeViewCategory_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            currentCategory = (WorkCategory)e.Node.Tag;
            LoadProducts();
        }

        #region addCategory
        string NEW_CATEGORY_NAME = "Нова Категорія";

        private void buttonAddCategory_Click(object sender, EventArgs e)
        { AddCategory(true); }

        private void buttonAddSubcategory_Click(object sender, EventArgs e)
        { AddCategory(false); }

        void AddCategory(bool isRoot)
        {
            WorkCategory category = new WorkCategory();
            category.WorkCategoryName = NEW_CATEGORY_NAME;
            context.WorkCategories.InsertOnSubmit(category);
            if (!isRoot) category.WorkCategory1 = currentCategory;
            root.Submit(false);
            LoadCategory();
        }
        #endregion

        private void buttonDeleteCategory_Click(object sender, EventArgs e)
        {
            if (currentCategory != null)
            {
                if (currentCategory.WorkCategories.Count > 0 || currentCategory.Works.Count > 0)
                { formMain.ShowError("Неможливо Видалити Категорію"); return; }
                context.WorkCategories.DeleteOnSubmit(currentCategory);
                root.Submit(false);
            }
            LoadCategory();
        }

        private void buttonSaveCategory_Click(object sender, EventArgs e)
        {
            if (currentCategory != null)
            {
                currentCategory.WorkCategoryName = textBoxCategoryName.Text;
                root.Submit(false);            
            }
            LoadCategory();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            if (currentCategory != null)
            {
                Work work = (Work)workBindingSource.AddNew();
                work.WorkCategory = currentCategory;
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            root.Submit(false);
            LoadProducts(); 
        }

        private void gridProduct_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string s = e.Value as string;
            Unit productUnit =
                (from x in unitBindingSource.OfType<Unit>() where x.ToString() == s select x).FirstOrDefault();
            e.Value = productUnit;
            e.ParsingApplied = true;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            Work work = (Work)workBindingSource.Current;
            if (work != null)
            {
                workBindingSource.RemoveCurrent();
                root.Submit(false);
            }
        }
    }
}
